package com.ruoyi.forum.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.forum.domain.ForumSchool;
import com.ruoyi.forum.domain.vo.SchoolRank;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.forum.service.IForumSchoolService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校管理Controller
 * 
 * @author ruoyi
 * @date 2024-02-01
 */
@RestController
@RequestMapping("/forum/school")
public class ForumSchoolController extends BaseController
{
    @Autowired
    private IForumSchoolService forumSchoolService;

    /**
     * 查询学校管理列表
     */
//    @PreAuthorize("@ss.hasPermi('forum:school:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(ForumSchool forumSchool)
    {
        startPage();
        if(forumSchool.getDelFlag()==null){
            forumSchool.setDelFlag("0");
        }
        List<ForumSchool> list = forumSchoolService.selectForumSchoolList(forumSchool);
        return getDataTable(list);
    }

    /**
     *前台系统-获取所有的学校
     */
    @GetMapping("/briefList")
    @Anonymous
    public TableDataInfo listForQian(ForumSchool forumSchool)
    {
        if(forumSchool.getDelFlag()==null){
            forumSchool.setDelFlag("0");
        }
        List<ForumSchool> list = forumSchoolService.selectForumSchoolList(forumSchool);
        return getDataTable(list);
    }

    /**
     * 查询当天学校热度排行
     */
    @Anonymous
    @GetMapping("/rank")
    public TableDataInfo list(@RequestParam("date") Date date)
    {
        System.out.println("要查询学校热度排行的时间："+date.toString());
        List<SchoolRank> list = forumSchoolService.selectForumSchoolRankForDate(date);
        return getDataTable(list);
    }

    /**
     * 导出学校管理列表
     */
    @PreAuthorize("@ss.hasPermi('forum:school:export')")
    @Log(title = "学校管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumSchool forumSchool)
    {
        List<ForumSchool> list = forumSchoolService.selectForumSchoolList(forumSchool);
        ExcelUtil<ForumSchool> util = new ExcelUtil<ForumSchool>(ForumSchool.class);
        util.exportExcel(response, list, "学校管理数据");
    }

    /**
     * 获取学校管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('forum:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(forumSchoolService.selectForumSchoolById(id));
    }

    /**
     * 新增学校管理
     */
    @PreAuthorize("@ss.hasPermi('forum:school:add')")
    @Log(title = "学校管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumSchool forumSchool)
    {
        forumSchool.setCreateBy(getUsername());
        return toAjax(forumSchoolService.insertForumSchool(forumSchool));
    }

    /**
     * 修改学校管理
     */
    @PreAuthorize("@ss.hasPermi('forum:school:edit')")
    @Log(title = "学校管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumSchool forumSchool)
    {
        forumSchool.setUpdateBy(getUsername());
        return toAjax(forumSchoolService.updateForumSchool(forumSchool));
    }

    /**
     * 删除学校管理
     */
    @PreAuthorize("@ss.hasPermi('forum:school:remove')")
    @Log(title = "学校管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumSchoolService.deleteForumSchoolByIds(ids));
    }
}
